package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import pojo.Cart;
import util.DataConn;

public class CartDao {
	private ComboPooledDataSource ds=new ComboPooledDataSource();
	private QueryRunner qr=new QueryRunner(ds);
	public int updateCartOrderId(int cid,String orderid) {
		int flag=0;
		String sql="update cart set orderId=? where c_id=?";
		try {
			flag=qr.update(sql,orderid,cid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public Cart getCartByUIDandPID(Cart cart){
		PreparedStatement ps;
		Cart c=null;
		Connection conn = DataConn.getConn();
		String sql ="select * from cart where p_id=? and c_name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, cart.getP_id());
			ps.setString(2, cart.getC_name());
			ResultSet rs=ps.executeQuery();
			if(rs.next()) {
				c = new Cart();
				c.setC_id(rs.getInt("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setP_id(rs.getString("p_id"));
				c.setQuantity(rs.getInt("quantity"));
				c.setP_price(rs.getFloat("p_price"));
				c.setAmount(rs.getFloat("amount"));
				c.setP_image(rs.getString("p_image"));
				c.setP_name(rs.getString("p_name"));
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
		
	}
	public int insertCart(Cart c) {
		int flag = 0;
		Connection conn = DataConn.getConn();
		String sql="insert into cart(c_name,p_id,quantity,p_price,amount,p_image,p_name)"
				+ "values(?,?,?,?,?,?,?)";
		//String sql="insert into cart values(null,?,?,?,?,?,?,?)";
		PreparedStatement pst;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, c.getC_name());
			pst.setString(2, c.getP_id());
			pst.setInt(3, c.getQuantity());
			pst.setFloat(4, c.getP_price());
			pst.setFloat(5, c.getP_price()*c.getQuantity());
			pst.setString(6, c.getP_image());
			pst.setString(7, c.getP_name());
			flag=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
		
	}
	//根据用户名查看购物车信息
	public List<Cart> showCarts(String cname) {
		List<Cart> list = new ArrayList<Cart>();
		Connection conn = DataConn.getConn();
		String sql = "select * from cart where c_name=?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, cname);
			ResultSet rs = ps.executeQuery();
			Cart c;
			while (rs.next()) {
				c = new Cart();
				c.setC_id(rs.getInt("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setP_id(rs.getString("p_id"));
				c.setQuantity(rs.getInt("quantity"));
				c.setP_price(rs.getFloat("p_price"));
				c.setAmount(rs.getFloat("amount"));
				c.setP_image(rs.getString("p_image"));
				c.setP_name(rs.getString("p_name"));
				list.add(c);// 把c添加到集合中
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}
	public int updateNumByCID(int cid,int num) {
		int flag=0;
		Connection conn = DataConn.getConn();
		String sql="update cart set quantity=? where c_id=?";
		PreparedStatement ps;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, num);//原有的数量上加一
			ps.setInt(2, cid);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public int updateNumByUIDandPID(Cart cart) {
		int flag=0;
		Connection conn = DataConn.getConn();
		String sql="update cart set quantity=? where p_id=? and c_name=?";
		PreparedStatement ps;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cart.getQuantity()+1);//原有的数量上加一
			ps.setString(2, cart.getP_id());
			ps.setString(3, cart.getC_name());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public int delCartByCID(String cid) {
		int flag =0;
		//连接数据库
		Connection conn =DataConn.getConn();
		//编写SQL语句
		String sql="delete from Cart where c_id=?";
		try {
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1,cid);
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public List<Cart> getOneCart(int c_id) {
		List<Cart> list=new ArrayList<Cart>();
			 Connection conn = DataConn.getConn();
				String sql = "select * from cart where c_id=?";  
				try {
					PreparedStatement pst = conn.prepareStatement(sql);
	                pst.setInt(1,c_id);
					ResultSet rs = pst.executeQuery();
					Cart c;
					if(rs.next()) {
						c = new Cart();
						c.setC_id(rs.getInt("c_id"));
						c.setC_name(rs.getString("c_name"));
						c.setP_id(rs.getString("p_id"));
						c.setQuantity(rs.getInt("quantity"));
						c.setP_price(rs.getFloat("p_price"));
						c.setAmount(rs.getFloat("amount"));
						c.setP_image(rs.getString("p_image"));
						c.setP_name(rs.getString("p_name"));
						list.add(c);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 return list;
		 }
}
